<?php

/**
 * EmsArticle form.
 *
 * @package    ems1
 * @subpackage form
 * @author     Jacobo Martínez
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class EmsArticleForm extends BaseEmsArticleForm
{
    public function configure()
    {
        unset ($this['content_id']);

        $this->setWidget('image', new sfWidgetFormInputHidden());

        $this->setValidator('image',new sfValidatorString(array('required' => false)));

        $this->setWidget('image_type', new sfWidgetFormInputHidden());

        $this->setValidator('image_type',new sfValidatorString(array('max_length' => 100, 'required' => false)));

        $this->setWidget('image_size', new sfWidgetFormInputHidden());

        $this->setValidator('image_size',new sfValidatorInteger(array('required' => false)));

        $this->setWidget('tmp_image', new sfWidgetFormInputFile());

        $this->setValidator('tmp_image',new sfValidatorString(array('required' => false)));

        if($this->getObject()->exists() && $this->getObject()->getImage()){
            $image = $this->getObject()->getImage();

            $folder = sfConfig::get('sf_upload_dir').'/portal_'.$this->getOption('portal_id').'/images/';

            if(is_file($folder.$image)){
                $image_size = filesize($folder.$image);

                $this->setDefaults(array('image_size' => $image_size));
            } else {
                $this->setDefaults(array('image_size' => 0));
            }
        }

        $this->setWidget('article', new sfWidgetFormTextareaTinyMCE(array(
                'config' => 'skin : "o2k7",theme_advanced_disable: "anchor,image,cleanup,help"',
            )));

        $this->setValidator('article', new sfValidatorString(array(
                        'required'   => true,
                        'min_length' => 10,
                    ), array(
                        'required' => 'Campo obligatorio. Debe agregar el contenido del artículo.'
                    )));

        $this->widgetSchema->setLabels(
            array(
                'tmp_image'    => 'Imagen',
                'article' => 'Artículo'
            ));
    }

    protected function doUpdateObject($values)
    {
        if(strcasecmp($values['image'],'') != 0){

            $path_parts_image = pathinfo($values['image']);

            $folder = sfConfig::get('sf_upload_dir').'/portal_'.$this->getOption('portal_id');

            if(!$this->getObject()->exists() || strcasecmp($path_parts_image['basename'], $this->getObject()->getImage()) != 0){
                $values['image'] = $this->saveUploadedimage(
                                    $path_parts_image['basename'],
                                    $values['image_type'],
                                    $values['image'],
                                    $values['image_size'],
                                    $folder.'/images/'
                                );
            }
        }

        unset(
            $values['image_type'], $values['image_size'], $values['tmp_image']
        );

        parent::doUpdateObject($values);
    }

    protected function saveUploadedimage($originalName, $type, $tempName, $size, $path) {

        $validated_image = new sfValidatedFile($originalName, $type, $tempName, $size, $path);

        return $validated_image->save();
    }
}
